package com.gqp.utils;

import com.gqp.engine.UnCompress;
import com.gqp.engine.UnRar;
import com.gqp.engine.UnSevenZ;
import com.gqp.engine.UnZip;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * gqp
 */
public class Utils {

    public static boolean isNotEmpty(String name) {
        return name != null && name.length() != 0;
    }

    public static boolean isEmpty(String name) {
        return !isNotEmpty(name);
    }

    /**
     * 转半角的函数(DBC case)
     * <p>
     * 全角空格为12288，半角空格为32
     * 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
     *
     * @param input 任意字符串
     * @return 半角字符串
     */
    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }


    public static boolean checkUnValue(String keyWord, String replaceKeyWord, Component parentComponent) {
        if (Utils.isEmpty(keyWord)) {
            JOptionPane.showMessageDialog(parentComponent, "请选择解压文件位置");
            return false;
        }

        if (Utils.isEmpty(replaceKeyWord)) {
            JOptionPane.showMessageDialog(parentComponent, "请选择解压文件解压后保存位置");
            return false;
        }
        return true;
    }

    public static boolean checkOnValue(String keyWord, String replaceKeyWord, Component parentComponent) {
        if (Utils.isEmpty(keyWord)) {
            JOptionPane.showMessageDialog(parentComponent, "请选择需要压缩文件位置");
            return false;
        }

        if (Utils.isEmpty(replaceKeyWord)) {
            JOptionPane.showMessageDialog(parentComponent, "请选择压缩后保存位置");
            return false;
        }
        return true;
    }

    public static String getUploadPath(Component parentComponent) {
        JFileChooser fileChooser = new JFileChooser();
        FileSystemView fsv = FileSystemView.getFileSystemView();
        System.out.println(fsv.getHomeDirectory());                //得到桌面路径
        fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
        fileChooser.setDialogTitle("请选择要上传的文件...");
        fileChooser.setApproveButtonText("确定");
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int result = 0;
        String path = null;
        result = fileChooser.showOpenDialog(null);
        if (JFileChooser.APPROVE_OPTION == result) {
            path = fileChooser.getSelectedFile().getPath();
        }
        return path;
    }


    public static String getDownloadPath(Component parentComponent) {
        //弹出文件选择框
        JFileChooser chooser = new JFileChooser();

        File file = null;
        //后缀名过滤器
//        FileNameExtensionFilter filter = new FileNameExtensionFilter(
//                ".zip", "zip");
//        FileNameExtensionFilter filter1 = new FileNameExtensionFilter(
//                ".7z", "7z");
//        FileNameExtensionFilter filter2 = new FileNameExtensionFilter(
//                ".rar", "rar");
//        chooser.setFileFilter(filter);
//        chooser.setFileFilter(filter1);

        //下面的方法将阻塞，直到【用户按下保存按钮且“文件名”文本框不为空】或【用户按下取消按钮】
        int option = chooser.showSaveDialog(null);
        if (option == JFileChooser.APPROVE_OPTION) {    //假如用户选择了保存
            file = chooser.getSelectedFile();
            String fname = chooser.getName(file);    //从文件名输入框中获取文件名

            //假如用户填写的文件名不带我们制定的后缀名，那么我们给它添上后缀
//            if (fname.indexOf(".zip") == -1) {
//                file = new File(chooser.getCurrentDirectory(), fname + ".zip");
//                System.out.println(file.getPath());
//            }
        }

        if (file != null) {
            return file.getPath();
        }

        return null;
    }


    public static String getFileType(String fileName) {
        //获取最后一个.的位置
        int lastIndexOf = fileName.lastIndexOf(".");
        //获取文件的后缀名 .jpg
        String suffixName = fileName.substring(lastIndexOf);
        return suffixName;
    }

    public static UnCompress getUnCompress(String filePath, String inputFile, String destDirPath) {
        UnCompress unCompress = null;
        switch (getFileType(filePath)) {
            case ".zip":
                unCompress = new UnZip(inputFile, destDirPath);
                break;
            case ".7z":
                unCompress = new UnSevenZ(inputFile, destDirPath);
                break;
            case ".rar":
                unCompress = new UnRar(inputFile, destDirPath);
                break;
            default:
                break;
        }
        return unCompress;
    }

}
